package danny.hoi.service.export.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import danny.hoi.dao.export.system.UserDao;
import danny.hoi.domain.export.system.User;
import danny.hoi.domain.export.system.UserExample;
import danny.hoi.service.export.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 企业crud服务提供商
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public List<User> findAll(UserExample example) {
        return userDao.selectByExample(example);
    }

    @Override
    public PageInfo<User> findByPages(UserExample example, Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<User> companies = findAll(example);

        return new PageInfo<>(companies);
    }

    @Override
    public User findById(String id) {
        return userDao.selectByPrimaryKey(id);
    }


    @Override
    public void save(User user) {
        //设置主键id
        user.setUserId(UUID.randomUUID().toString().replaceAll("-", ""));
        userDao.insertSelective(user);
    }

    @Override
    public void update(User user) {
        userDao.updateByPrimaryKeySelective(user);
    }

    @Override
    public boolean delete(String id) {
        //查询当前用户有没有被关联
        long count = userDao.selectUserRoleById(id);
        if (count > 0) {
            return false;
        } else {
            userDao.deleteByPrimaryKey(id);
            return true;
        }
    }
}
